# 0.0 Load libraries ----
library(tidyverse)
library(rio)
library(scales)
library(estimatr)
library(MASS)
library(marginaleffects)
library(modelsummary)

# 1.0 Read data ----
ds    <- import("data/CCES22-CAC-OUTPUT.sav")
state <- import("data/states.csv")
mc117 <- import("data/mc117.csv")
mc118 <- import("data/mc118.csv")

# 3.0 Include supplemental data ----
ds <- ds %>% 
  left_join(state, by = c("inputstate")) %>% 
  left_join(mc117, by = c("state","cdid117")) %>% 
  left_join(mc118, by = c("state","cdid118"))

# 3.0 State definitions ----
states <- c(
  "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
  "Connecticut", "Delaware", "DC", "Florida", "Georgia", "Hawaii", "Idaho",
  "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", 
  "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
  "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
  "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
  "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
  "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
  "Washington", "West Virginia", "Wisconsin", "Wyoming"
)

redist_comm <- c(
  "Arizona", "California", "Colorado", "Hawaii", "Idaho", "Michigan", "Montana", 
  "New Jersey", "Washington"
)

redist_leg <- c(
  "Alabama", "Arkansas", "Florida", "Georgia", "Illinois", "Indiana", "Iowa",
  "Kansas", "Kentucky", "Louisiana", "Maine", "Missouri", "Maryland", 
  "Massachusetts", "Mississippi", "Nebraska", "Nevada", "New Mexico", "Ohio", 
  "Oklahoma", "Oregon", "Rhode Island", "South Carolina", "Tennessee", "Texas",
  "Utah", "West Virginia"
)

redist_court <- c(
  "Connecticut", "Minnesota", "New York", "New Hampshire", "North Carolina",
  "Pennsylvania", "Virginia", "Wisconsin"
)

redist_single <- c(
  "Alaska", "Delaware", "North Dakota", "South Dakota", "Vermont", "Wyoming"
  )

# 4.0 Clean data ----
ds <- ds %>%
  mutate(
    redist_knowledge = factor(
      CAC316, 
      levels = c(1, 2, 3, 9), 
      labels = c("lot", "little", "nothing", "NA")
    ),
    redist_type_answer = factor(
      CAC317, 
      levels = c(1, 2, 3, 9, 98, 99), 
      labels = c(
        "state_leg", "court", "commission", "dont_know", "skipped", "NA"
      )
    ),
    redist_satisfied_f_post = factor(
      CAC418, 
      levels = c(-1, 1, 2, 3, 4, 5, 9, 99), 
      labels = c(
        "no_data", "very_satisfied", "somewhat_satisfied", "neutral", 
        "somewhat_dissatisfied", "very_dissatisfied", "dont_know", "NA"
      )
    ),
    redist_satisfied_num_post = case_when(
      redist_satisfied_f_post == "very_satisfied"        ~ 5,
      redist_satisfied_f_post == "somewhat_satisfied"    ~ 4,
      redist_satisfied_f_post == "neutral"               ~ 3,
      redist_satisfied_f_post == "somewhat_dissatisfied" ~ 2,
      redist_satisfied_f_post == "very_dissatisfied"     ~ 1
    ),
    redist_satisfied_fac_post = factor(redist_satisfied_num_post),
    redist_fair_f_post = factor(
      CAC419, 
      levels = c(-1, 1, 2, 3, 4, 9, 99),
      labels = c(
        "no_data", "great_deal", "good_amount", 
        "not_much", "not_at_all", "dont_know", "NA"
      )
    ),
    redist_fair_num_post = case_when(
      redist_fair_f_post == "great_deal"  ~ 4,
      redist_fair_f_post == "good_amount" ~ 3,
      redist_fair_f_post == "not_much"    ~ 2,
      redist_fair_f_post == "not_at_all"  ~ 1
    ),
    redist_fair_fac_post = factor(redist_fair_num_post), 
    satisfied_scaled = rescale(redist_satisfied_num_post), 
    fairness_scaled = rescale(redist_fair_num_post), 
    state_name = factor(
      inputstate,
      levels = c(1:2, 4:6, 8:13, 15:42, 44:51, 53:56),
      labels = states
    ), 
    redist_type_2020_draw = case_when(
      state_name %in% redist_comm   ~ "Commission", 
      state_name %in% redist_leg    ~ "State_Leg",
      state_name %in% redist_court  ~ "Court",
      state_name %in% redist_single ~ "Single_Seat"
    ),
    redist_type_2020_draw = factor(
      redist_type_2020_draw, 
      levels = c("Commission", "State_Leg", "Court", "Single_Seat")
    ), 
    redist_type_two = case_when(
      redist_type_2020_draw == "Commission" ~ "Commission",
      redist_type_2020_draw == "State_Leg" ~ "State_Leg",
      redist_type_2020_draw == "Single_Seat" ~ "Single_Seat", 
      redist_type_2020_draw == "Court" & state_name %in% c(
        "Connecticut", "Minnesota", "North Carolina", 
        "New Hampshire", "Pennsylvania", "Wisconsin"
      ) ~ "State_Leg", 
      redist_type_2020_draw == "Court" & state_name %in% c(
        "Virginia","New York"
      ) ~ "Commission",
    ), 
    redist_type_two = factor(
      redist_type_two, 
      levels = c("Commission", "State_Leg", "Single_Seat")
    ), 
    redist_party_control = case_when(
      redist_type_2020_draw == "Commission" ~ "Commission",
      redist_type_2020_draw == "Court" ~ "Court",
      redist_type_2020_draw == "Single_Seat" ~ "Single_Seat",
      redist_type_2020_draw == "State_Leg" & state_name %in% c(
        "Illinois", "Maine", "Maryland", "Massachusetts",
        "Nevada", "New Mexico", "Oregon", "Rhode Island"
      ) ~ "Dem",
      redist_type_2020_draw == "State_Leg" & state_name %in% c(
        "Alabama", "Arkansas", "Florida", "Georgia", "Indiana",
        "Iowa", "Kansas", "Kentucky", "Louisiana", "Missouri",
        "Mississippi", "Nebraska", "Ohio", "Oklahoma", "South Carolina",
        "Tennessee", "Texas", "Utah", "West Virginia"
      ) ~ "Rep"
    ), 
    redist_party_control_2 = case_when(
      redist_type_2020_draw == "Court" ~ "Court",
      redist_type_2020_draw == "Single_Seat" ~ "Single_Seat",
      redist_type_2020_draw == "State_Leg" & state_name %in% c(
        "Illinois", "Maine", "Maryland", "Massachusetts",
        "Nevada", "New Mexico", "Oregon", "Rhode Island"
      ) ~ "Dem",
      redist_type_2020_draw == "State_Leg" & state_name %in% c(
        "Alabama", "Arkansas", "Florida", "Georgia", "Indiana",
        "Iowa", "Kansas", "Kentucky", "Louisiana", "Missouri",
        "Mississippi", "Nebraska", "Ohio", "Oklahoma", "South Carolina",
        "Tennessee", "Texas", "Utah", "West Virginia"
      ) ~ "Rep",
      redist_type_2020_draw == "Commission" & state_name %in% c(
        "California", "Colorado", "Hawaii", "New Jersey", "Washington"
      ) ~ "Dem_Commission",
      redist_type_2020_draw == "Commission" & state_name %in% c(
        "Arizona", "Idaho", "Michigan", "Montana"
      ) ~ "Rep_Commission"
    ), 
    redist_partisan_or_not = case_when(
      state_name %in% c(
        "Arizona", "California", "Colorado", "Connecticut", "Hawaii", "Idaho",
        "Michigan", "Minnesota", "Pennsylvania", "Maine", "Montana", 
        "New Jersey", "Virginia", "Washington", "Wisconsin"
      ) ~ "non_partisan",
      state_name %in% c(
        "Alabama", "Arkansas", "Florida", "Georgia", "Illinois", "Indiana", 
        "Iowa", "Kansas", "Kentucky", "Louisiana", "Missouri", "Maryland", 
        "Massachusetts", "North Carolina", "Mississippi", "Nebraska", "Nevada", 
        "New Hampshire", "New Mexico", "New York", "Ohio", "Oklahoma", "Oregon",
        "Rhode Island", "South Carolina", "Tennessee", "Texas", "Utah", 
        "West Virginia"
      ) ~ "partisan",
      state_name %in% redist_single ~ "single"
    ), 
    redist_partisan_or_not = factor(
      redist_partisan_or_not, 
      levels = c("partisan", "non_partisan", "single")
    ), 
    redist_comission_type = case_when(
      state_name %in% c(
        "Arizona", "California", "Colorado", "Idaho", "Michigan", "Montana",
        "New Jersey", "Washington"
      ) ~ "Ind_Commission",
      state_name %in% c(
        "Alabama", "Arkansas", "Florida", "Georgia", "Illinois", "Indiana",
        "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Missouri",
        "Maryland", "Massachusetts", "Minnesota", "Mississippi", "Nebraska",
        "Nevada", "New Hampshire", "New Mexico", "North Carolina", "Ohio",
        "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
        "Tennessee", "Texas", "Utah", "West Virginia", "Wisconsin"
      ) ~ "State_Leg",
      state_name %in% c(
        "Connecticut", "Hawaii", "New York", "Virginia"
      ) ~ "Political_Commission",
      state_name %in% redist_single ~ "Single_Seat"
    ), 
    redist_correct = case_when(
      redist_type_2020_draw == "State_Leg" & 
        redist_type_answer == "state_leg" ~ "yes",
      redist_type_2020_draw == "State_Leg" & 
        redist_type_answer %in% c("court", "commission", "dont_know") ~ "no",
      redist_type_2020_draw == "Commission" & 
        redist_type_answer == "commission" ~ "yes",
      redist_type_2020_draw == "Commission" & 
        redist_type_answer %in% c("court", "state_leg", "dont_know") ~ "no",
      redist_type_2020_draw == "Court" & 
        redist_type_answer == "court" ~ "yes",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "Commission" & 
        redist_type_answer == "state_leg" ~ "no",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "State_Leg" & 
        redist_type_answer == "state_leg" ~ "yes",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "Commission" & 
        redist_type_answer == "commission" ~ "yes",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "State_Leg" & 
        redist_type_answer == "commission" ~ "no",
      redist_type_2020_draw == "Court" & 
        redist_type_answer == "dont_know" ~ "no"
    ), 
    redist_correct_three = case_when(
      redist_type_2020_draw == "State_Leg" & 
        redist_type_answer == "state_leg" ~ "yes",
      redist_type_2020_draw == "State_Leg" & 
        redist_type_answer %in% c("court", "commission") ~ "no",
      redist_type_2020_draw == "Commission" & 
        redist_type_answer == "commission" ~ "yes",
      redist_type_2020_draw == "Commission" & 
        redist_type_answer %in% c("court", "state_leg") ~ "no",
      redist_type_2020_draw == "Court" & 
        redist_type_answer == "court" ~ "yes",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "Commission" & 
        redist_type_answer == "state_leg" ~ "no",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "State_Leg" & 
        redist_type_answer == "state_leg" ~ "yes",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "Commission" & 
        redist_type_answer == "commission" ~ "yes",
      redist_type_2020_draw == "Court" & 
        redist_type_two == "State_Leg" & 
        redist_type_answer == "commission" ~ "no",
      redist_type_2020_draw %in% c("State_Leg", "Commission", "Court") & 
        redist_type_answer == "dont_know" ~ "dont_know"
    ), 
    pid = case_when(
      pid7 %in% 1:2 ~ "dem", 
      pid7 %in% 3:5 ~ "ind", 
      pid7 %in% 6:7 ~ "rep"
    ), 
    pid = factor(pid, levels = c("rep", "ind", "dem")), 
    pid_lean = case_when(
      pid7 %in% 1:3 ~ "dem", 
      pid7  ==  4   ~ "ind", 
      pid7 %in% 5:7 ~ "rep"
    ), 
    ideo = ifelse(ideo5 == 6, NA, ideo5), 
    woman = ifelse(gender4 == 2, 1, 0), 
    age = 2022 - birthyr,
    resp_race = case_when(
      race==1 ~ "white",
      race==2 ~ "black",
      race==3 ~ "hispanic",
      race==4 ~ "asian",
      race==5 ~ "other",
      race==6 ~ "multiracial",
      race==7 ~ "other",
      race==8 ~ "other"
    ), 
    resp_gender = case_when(
      gender4 == 1 ~ "man", 
      gender4 == 2 ~ "woman", 
      gender4 == 3 ~ "Non-binary"
    ), 
    resp_income = case_when(
      faminc_new %in% 1:4   ~ "Less than $40,000",
      faminc_new %in% 5:8   ~ "$40,000 - $79,999",
      faminc_new %in% 9:12  ~ "$80,000 - $199,999",
      faminc_new %in% 13:16 ~ "$200,000 or more",
      faminc_new  ==  97    ~ "Prefer not to say"
    ), 
    resp_income = factor(
      resp_income, 
      levels = c(
        "Less than $40,000", "$40,000 - $79,999", "$80,000 - $199,999",
        "$200,000 or more", "Prefer not to say"
      )
    ), 
    resp_income_num = case_when(
      faminc_new %in% 1:4   ~ 1,
      faminc_new %in% 5:8   ~ 2,
      faminc_new %in% 9:12  ~ 3,
      faminc_new %in% 13:16 ~ 4,
    ), 
    resp_educ = as.numeric(educ), 
    white = ifelse(race == 1, 1, 0), 
    black = ifelse(race == 2, 1, 0), 
    hispanic = ifelse(race == 3, 1, 0), 
    asian = ifelse(race == 4, 1, 0), 
    religiosity = pew_churatd + pew_religimp + pew_prayer, 
    mc_party17 = factor(
      party_code17, levels = c("100", "200"), labels = c("dem", "rep")
    ), 
    mc_party18 = factor(
      party_code18, levels = c("100", "200"), labels = c("dem", "rep")
    ), 
    party_match17 = case_when(
      pid == "dem" & mc_party17 == "dem" ~ "Copartisan", 
      pid == "rep" & mc_party17 == "rep" ~ "Copartisan",
      pid == "dem" & mc_party17 == "rep" ~ "Opposition",
      pid == "rep" & mc_party17 == "dem" ~ "Opposition",
      pid == "ind" ~ "Independent"
    ), 
    party_match17 = factor(
      party_match17, levels = c("Copartisan", "Independent", "Opposition")
    ), 
    party_match18 = case_when(
      pid == "dem" & mc_party18 == "dem" ~ "Copartisan", 
      pid == "rep" & mc_party18 == "rep" ~ "Copartisan",
      pid == "dem" & mc_party18 == "rep" ~ "Opposition",
      pid == "rep" & mc_party18 == "dem" ~ "Opposition",
      pid == "ind" ~ "Independent"
    ), 
    party_match18 = factor(
      party_match18, levels = c("Copartisan", "Independent", "Opposition")
    ), 
    party_match18_lean = case_when(
      pid_lean == "dem" & mc_party18 == "dem" ~ "Copartisan", 
      pid_lean == "rep" & mc_party18 == "rep" ~ "Copartisan",
      pid_lean == "dem" & mc_party18 == "rep" ~ "Opposition",
      pid_lean == "rep" & mc_party18 == "dem" ~ "Opposition",
      pid_lean == "ind" ~ "Independent"
    ), 
    party_match18_lean = factor(
      party_match18_lean, levels = c("Copartisan", "Independent", "Opposition")
    ), 
    mc_change = case_when(
      party_match17 == party_match18 ~ "No_change", 
      party_match17=="Copartisan" & party_match18 =="Opposition" ~ "Lost",
      party_match17=="Opposition" & party_match18 =="Copartisan" ~ "Gained"
    ), 
    dist17 = paste(state, cdid117, sep = "_"), 
    dist18 = paste(state, cdid118, sep = "_"), 
    new_dist = ifelse(dist17 == dist18, "no", "yes"), 
    redist_party_control_match = case_when(
      redist_party_control == "Dem" & pid =="dem" ~ "Copartisan",
      redist_party_control == "Rep" & pid =="rep" ~ "Copartisan",
      redist_party_control == "Dem" & pid =="rep" ~ "Opposition",
      redist_party_control == "Rep" & pid =="dem" ~ "Opposition",
      redist_party_control %in% c("Dem", "Rep") & pid == "ind" ~ "Independent", 
      redist_party_control == "Commission" ~ "Commission",
      redist_party_control == "Court" ~ "Court",
      redist_party_control == "Single_Seat" ~ "Single_Seat"
    ), 
    redist_party_control_match = factor(
      redist_party_control_match, 
      levels = c(
        "Opposition", "Independent", "Copartisan",
        "Commission", "Court", "Single_Seat"
      )
    ), 
    redist_party_control_match_2 = case_when(
      redist_party_control_2 == "Dem" & pid == "dem" ~ "Copartisan",
      redist_party_control_2 == "Rep" & pid == "rep" ~ "Copartisan",
      redist_party_control_2 == "Dem" & pid == "rep" ~ "Opposition",
      redist_party_control_2 == "Rep" & pid == "dem" ~ "Opposition",
      redist_party_control_2 %in% c("Dem", "Rep") & 
        pid == "ind" ~ "Independent", 
      redist_party_control_2 == "Dem_Commission" & 
        pid=="dem" ~ "Com_Copartisan_SL",
      redist_party_control_2 == "Dem_Commission" & 
        pid=="rep" ~ "Com_Opposition_SL",
      redist_party_control_2 == "Rep_Commission" & 
        pid=="dem" ~ "Com_Opposition_SL",
      redist_party_control_2 == "Rep_Commission" & 
        pid=="rep" ~ "Com_Copartisan_SL",
      redist_party_control_2 %in% c("Dem_Commission", "Rep_Commission") & 
        pid=="ind" ~ "Com_Independent_SL",
      redist_party_control_2 == "Court" ~ "Court",
      redist_party_control_2 == "Single_Seat" ~ "Single_Seat"
    ), 
    redist_party_control_match_2 = factor(
      redist_party_control_match_2, 
      levels = c(
        "Opposition", "Independent", "Copartisan", "Com_Copartisan_SL", 
        "Com_Opposition_SL", "Com_Independent_SL", "Court", "Single_Seat"
      )
    )
  )
